#include "StdAfx.h"
#include "TouchPad.h"

#define KeyPressed(key) (GetAsyncKeyState(key)&0x8000)

int		CTouchPad::m_padX;
int		CTouchPad::m_padY;
BOOL	CTouchPad::m_touched;
BOOL	CTouchPad::m_running;
	
void CTouchPad::init()
{
	TPCalibrateParam calibrate;
    TP_Init();

    if (!TP_GetUserInfo(&calibrate))
    {
        OS_Panic("FATAL ERROR: can't get UserInfo\n");
    }
    else
    {
        OS_Printf("Get Calibration Parameter from NVRAM\n");
    }

    TP_SetCalibrateParam(&calibrate);
    
    m_running = FALSE;
}

void CTouchPad::start()
{
	m_running = TRUE;
}

void CTouchPad::stop()
{
	m_running = FALSE;
}

void CTouchPad::update()
{
	if (!m_running)
		return;

	TPData raw_point, disp_point;
	
	while( TP_RequestRawSampling( &raw_point ) != 0)
	{
	};
	TP_GetCalibratedPoint( &disp_point, &raw_point);


	m_touched = disp_point.touch;
	
	if (m_touched)
	{
		if (disp_point.validity == TP_VALIDITY_VALID)
		{
			m_padX = disp_point.x;
			m_padY = disp_point.y;
		}
	}
	else
	{
		m_padX = -1;
		m_padY = -1;		
	}
}